import {
  login,
  userInfo
} from "@/api/account";
import {
  saveToken,
  cleanToken
} from "@/utils/auth";
import router from "@/router";
const state = {
  account: "",
  menuList: [],
  name: "",
  avatar: require("@/assets/image/head.png")
};
const mutations = {
  saveUserInfo(state, {
    account,
    menuList,
    name
  }) {
    state.account = account;
    state.menuList = menuList;
    state.name = name;
  },
  logout(state) {
    state.account = '';
    state.menuList = [];
    state.name = '';
    cleanToken()
    router.push({
      path: "/login"
    });
  }
};
const actions = {
  async login(_, user) {
    const username = user.username.trim();
    const token = await login(username, user.password);
    saveToken(token);
    router.push({
      path: "/"
    });
  },
  async getUserInfo({
    commit
  }) {
    const data = await userInfo();
    commit("saveUserInfo", data);
  }
};
const getters = {
  menu: state => state.menuList.filter(_ => _.accessStatus === true)
}
export default {
  state,
  mutations,
  actions,
  getters
};